package com.zim.analysis.saika;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.zim.analysis.po.PhotoPO;

public class Image0003 extends BaseHandle {

    public Image0003(byte[] bytes) throws Exception {
        super(bytes);
    }

    public PhotoPO analysis() throws Exception {
        PhotoPO photo = new PhotoPO();
        photo.setMessage(stringMsg);
        int length = validate();
        photo.setLength(length);
        length -= 20;
        photo.setDeviceName(stringMsg.substring(2, 22));
        photo.setSerialNumber(message[11]);
        photo.setTime(getTime(length));

        switch (message[16]) {
        case 0x00:
            photo.setPhotoType("JPEG");
            break;
        case 0x01:
            photo.setPhotoType("GIF");
            break;
        default:
            break;
        }
        photo.setSize(message[17] & 0xFF);
        photo.setNumber(message[18] & 0xFF);
        photo.setBis(stringMsg.substring(38, 38 + length * 2));

        double longitude = Double.parseDouble(stringMsg.substring(50 + length * 2, 53 + length * 2))
                + Double.parseDouble("0." + stringMsg.substring(53 + length * 2, 60 + length * 2)) / 60 * 100;
        double latitude = Double.parseDouble(stringMsg.substring(60 + length * 2, 62 + length * 2))
                + Double.parseDouble("0." + stringMsg.substring(62 + length * 2, 68 + length * 2)) / 60 * 100;
        photo.setLongitude(longitude);
        photo.setLatitude(latitude);

        return photo;
    }

    protected Long getTime(int length) throws Exception {
        String timeString = "20" + stringMsg.substring(38 + length * 2, 50 + length * 2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = sdf.parse(timeString.toString());
        long time = date.getTime();
        if (time < 1511008433000L) {
            time = System.currentTimeMillis();
        }
        return time;
    }

}
